/**
 * 
 */
package com.wisdytech.linkdcs.equipment.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.event.DcsEquipEvent;
import com.wisdytech.common.bean.event.DcsParamEvent;
import com.wisdytech.common.enums.ActionType;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqClientDao;
import com.wisdytech.linkdcs.equipment.dao.IEqCollectionParamDao;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.dao.IEqToClientDao;
import com.wisdytech.linkdcs.equipment.dto.EquipConnectDTO;
import com.wisdytech.linkdcs.equipment.model.Client;
import com.wisdytech.linkdcs.equipment.model.EquipmentToClient;
import com.wisdytech.linkdcs.equipment.service.IEquipmentConnectService;
import com.wisdytech.linkdcs.equipment.vo.EquipmentConnectVO;

/**
 * <p>
 * ClassName: EquipmentConnectServiceImpl
 * </p>
 * <p>
 * Description: 连接设备管理的业务实现层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-30
 * </p>
 */
@Service("equipmentConnectService")
@Transactional
public class EquipmentConnectServiceImpl extends BaseService<IEqClientDao, Client> implements IEquipmentConnectService {
    /**
     * 注入终端dao
     */
    @Resource
    private IEqClientDao clientDao;
    /**
     * 注入设备dao
     */
    @Resource
    private IEqEquipmentDao equipmentDao;
    
    
    @Resource
    private IEqCollectionParamDao collectionParamDao;
    
    /**设备关联关系*/
    @Resource
    private IEqToClientDao eqToClientDao;
    
    
    /**处理设备注册事件推送*/
    @Autowired
    ApplicationContext applicationContext;
    
    
   /* *//**
     * 设备dcs推送服务
     *//*
    @Resource
    private IEquipmentInfoService infoService;

    *//**
     * 设备dcs推送服务
     *//*
    @Resource
    private IEquipmentCollectionInfoService equipmentCollectionInfoService;
    
    @Autowired
    private IEquipmentMqDcsService iEquipmentMqDcsService;*/
    
    
    
    @Override
    public StandardResult addEquipmentConnect(EquipmentToClient clientConnect, String loginName) {
    	
    	
    	//判断是否存在
    	EntityWrapper<EquipmentToClient> ew =new EntityWrapper<>();
    	Integer count=eqToClientDao.selectCount(
    			ew.eq("client_id", clientConnect.getClientId())
    				.eq("EQUIP_CODE", clientConnect.getEquipCode())
    					.eq("type", clientConnect.getType()));
    	System.out.println(count);
    	if(count >0) {
    		return StandardResult.getFailResult("采集方式连接已存在");
    	}
    	
    	String eqCollectionId =UUID.randomUUID().toString();
    	clientConnect.setPkId(eqCollectionId);
    	eqToClientDao.insert(clientConnect);
    	
    	
    	//事件注册
    	//设备信息注册
    	/*Client client =clientDao.selectById(clientConnect.getClientId());
    	clientConnect.setClientRoute(client.getClientAddress());*/
    	EquipmentToClient detailClient =
    			eqToClientDao.selectDetailById(clientConnect.getPkId());
    	
    	
    	DcsEquipEvent dcsEquipEvent =new DcsEquipEvent(this,detailClient,ActionType.ADD);
    	applicationContext.publishEvent(dcsEquipEvent);
    	DcsParamEvent dcsParamEvent =new DcsParamEvent(this, detailClient, ActionType.ADD);
    	applicationContext.publishEvent(dcsParamEvent);
    	
    	/*
    	infoService.add(eqCollectionId);
    	equipmentCollectionInfoService.add(eqCollectionId);
    	iEquipmentMqDcsService.add(eqCollectionId);*/
    	
    	return StandardResult.getSuccessResult();
    	
        
    }

    @Override
    public StandardResult updateEquipmentConnect(EquipmentToClient clientConnect, String loginName) {
    	
    	//查询当前类型是否相同
    	
    	EquipmentToClient toClient =
    			eqToClientDao.selectById(clientConnect.getPkId());
    	
    	EntityWrapper<EquipmentToClient> ew =new EntityWrapper<>();
    	//匹配是否已经存在相关的信息
    	List<EquipmentToClient>  etcLists=eqToClientDao.selectList(
    			ew.eq("client_id", clientConnect.getClientId())
				.eq("EQUIP_CODE", clientConnect.getEquipCode())
					.eq("type", clientConnect.getType()));
    	
    	
    	//判断是否存在
    	if(etcLists.size()>0 
    			&& !etcLists.get(0).getPkId().equals(clientConnect.getPkId())
    				) {
    		return StandardResult.getFailResult("类型已存在");
    	}
    	
    	//修改
    	eqToClientDao.updateAllColumnById(clientConnect);
    	
    
    	//事件注册
    	EquipmentToClient detailClient =
    			eqToClientDao.selectDetailById(clientConnect.getPkId());
    	DcsEquipEvent dcsEquipEvent =new DcsEquipEvent(this,detailClient,ActionType.UPDATE);
    	applicationContext.publishEvent(dcsEquipEvent);
    	DcsParamEvent dcsParamEvent =new DcsParamEvent(this, detailClient, ActionType.UPDATE);
    	applicationContext.publishEvent(dcsParamEvent);
    	
    	
    	return StandardResult.getSuccessResult();
    	
    }

    @Override
    public PageInfo<EquipmentToClient> listEquipmentConnectByFilter(EquipConnectDTO connectDTO, Filter filter) {
    	
    	
        List<EquipmentToClient> lists =
                super.beforeList(IEqToClientDao.class, filter).selectConnectPage(connectDTO);
        
        
        PageInfo<EquipmentToClient> equipModelPage=new PageInfo<>(lists);
        return equipModelPage;
    }

    @Override
    public EquipmentConnectVO getEquipmentCollectById(String pkId) {
    	
        
    	EquipmentToClient client =eqToClientDao.selectDetailById(pkId);
        EquipmentConnectVO connectVO = DataTransformUtil.changeEquipmentConnectVO(client);
        return connectVO;
    }

    @Override
    public StandardResult delEquipmentConnect(List<String> toClientPkIds) {
    	
    	
    	//先查询出设备,方便推送
    	List<EquipmentToClient> clients =new ArrayList<>();
    	for(String eqClientId : toClientPkIds) {
    		/*EquipmentToClient clientConnect= eqToClientDao.selectDetailById(eqClientId);
    		Client client =clientDao.selectById(clientConnect.getClientId());
        	clientConnect.setClientRoute(client.getClientAddress());*/
    		EquipmentToClient detailClient =
        			eqToClientDao.selectDetailById(eqClientId);
    		clients.add(detailClient);
    	}
    	
    	eqToClientDao.deleteBatchIds(toClientPkIds);
    	
    	//删除成功后推送
    	for(EquipmentToClient client : clients) {
    		DcsEquipEvent dcsEquipEvent =new DcsEquipEvent(this,client,ActionType.DEL);
        	applicationContext.publishEvent(dcsEquipEvent);
        	DcsParamEvent dcsParamEvent =new DcsParamEvent(this, client, ActionType.DEL);
        	applicationContext.publishEvent(dcsParamEvent);
    	}
    	
    	

    	return StandardResult.getSuccessResult();
       
        
    }

	@Override
	public int countEquipmentCollectByClient(String clientId) {
		EntityWrapper<EquipmentToClient> ew =new EntityWrapper<>();
		ew.eq("client_id", clientId);
		return eqToClientDao.selectCount(ew);
	}
}
